package Database;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class DataBaseAccessor {
	
	 public static Logger log = Logger.getLogger("log_file");
	 FileHandler hand;
	
	Statement statement;
	
	public DataBaseAccessor() {
		DataBaseConnector dbconnect=new DataBaseConnector();
		statement=dbconnect.getstatement();
		
		try {
			hand = new FileHandler("MonLog.txt");
			log.addHandler(hand);
			SimpleFormatter formatter=new SimpleFormatter();
			hand.setFormatter(formatter);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	public ResultSet select(String query){
		try {
			log.info("Requette db => "+query);
			ResultSet resultat =statement.executeQuery(query);
			return resultat;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public void insertionSelect(){
		try {
			statement.executeUpdate("insert into typeInformation values(1,'Position')");
			statement.executeUpdate("insert into typeInformation values(2,'Notification')");
			statement.executeUpdate("insert into typeInformation values(3,'Incident')");
			statement.executeUpdate("insert into Information values(1,1,'Train à la station Auber')");
			statement.executeUpdate("insert into Information values(2,2,'Temperature trop elevee')");
			statement.executeUpdate("insert into station values(1,'La defense')");
			statement.executeUpdate("insert into ligne values(1,'14')");
			statement.executeUpdate("insert into typeSource values(1,'train')");
			statement.executeUpdate("insert into Source values(1,'CP01',1,1,1)");
			statement.executeUpdate("insert into Source values(2,'CP02',2,1,3)");
			//ResultSet result=statement.executeQuery("select * from typeinformation where idtype=1");
			//result.next();
			//System.out.println(result.getInt("idtype")+"  "+result.getNString("libelle"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void createTablePrimK(){
		try {
			statement.executeUpdate("drop table if exists Information");
			statement.executeUpdate("drop table if exists station");
			statement.executeUpdate("drop table if exists ligne");
			statement.executeUpdate("drop table if exists typesource");
			statement.executeUpdate("drop table if exists typeinformation");
			statement.executeUpdate("drop table if exists Source");
			statement.executeUpdate("create table TypeInformation(idtype int,libelle varchar(30), primary key (idtype))");
			statement.executeUpdate("create table Information(idInformation int, typeInformation int,description varchar(500), primary key (idInformation))");
			statement.executeUpdate("create table Station(idstation int,libelle varchar(30), primary key (idstation))");
			statement.executeUpdate("create table Ligne(idligne int,libelle varchar(30), primary key (idligne))");
			statement.executeUpdate("create table TypeSource(idtype int,libelle varchar(30), primary key (idtype))");
			statement.executeUpdate("create table Source(idsource int,libelle varchar(30)," +
					"idstation int, idtype int, idligne int,"+
					" primary key (idsource)" +
					");");
			System.out.println("table créée !!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
